from datetime import datetime, timezone
import requests
import pandas as pd
from utils import df_into_db


def timestamp_to_utc_str(timestamp, fmt="%Y-%m-%d %H:%M:%S"):
    """
    将时间戳转换为UTC时间字符串

    参数:
        timestamp: Unix时间戳（秒级）=]
        fmt: 输出字符串的格式
    返回:
        str: UTC时间字符串
    """
    # 创建UTC时间对象
    utc_dt = datetime.fromtimestamp(timestamp, tz=timezone.utc)

    # 格式化为字符串
    return utc_dt.strftime(fmt)


def get_complete_historical_data():
    """
    分批获取从最早到现在的完整5分钟K线数据
    """
    base_url = "https://www.bitstamp.net/api/v2/ohlc/btcusd/"
    all_data = []

    # 从 Bitstamp 开始运营的时间点
    current_end = 1757894400
    batch_size = 1000  # 每次获取1000条
    step = 300  # 5分钟

    # 计算最早可获取的时间戳 (2011-08-18)
    # earliest_timestamp = 1313664000  # 2011-08-18 13:00:00 UTC
    earliest_timestamp = 1448198100

    print("开始获取完整历史数据...")
    print(f"时间范围: {datetime.fromtimestamp(earliest_timestamp)} 到 {datetime.fromtimestamp(current_end)}")

    current_start = earliest_timestamp

    while current_start < current_end:
        # 计算这批数据的结束时间
        batch_end = min(current_start + (batch_size * step), current_end)

        params = {
            'step': step,
            'limit': batch_size,
            'start': current_start,
            'end': batch_end
        }

        try:
            response = requests.get(base_url, params=params, timeout=30)
            data = response.json()

            if 'data' in data and 'ohlc' in data['data']:
                batch_data = data['data']['ohlc']

                if not batch_data:
                    break  # 没有更多数据

                all_data.extend(batch_data)
                print(
                    f"已获取 {len(batch_data)} 条数据, 时间: {datetime.fromtimestamp(int(batch_data[0]['timestamp']))}")

                # 更新起始时间为这批数据的最后时间戳
                current_start = int(batch_data[-1]['timestamp']) + step

                # 添加延迟避免速率限制
                import time
                time.sleep(0.5)
            else:
                print("未找到数据，可能已到达最新")
                break

        except Exception as e:
            print(f"获取数据失败: {e}")
            break

    # 转换为DataFrame
    if all_data:
        df = pd.DataFrame(all_data)
        df["datetime"] = df["timestamp"].apply(lambda x: timestamp_to_utc_str(int(x)))
        df['timestamp'] = df["timestamp"].apply(lambda x: int(x)*1000)
        df['open'] = df['open'].astype(float)
        df['high'] = df['high'].astype(float)
        df['low'] = df['low'].astype(float)
        df['close'] = df['close'].astype(float)
        df["symbol"] = "BTC"
        df["type"] = "spot"
        df["datasource"] = "bitstamp"
        df["frequency"] = "5m"
        df.drop(columns=["volume"], axis=1, inplace=True)
        print("drop前长度:", len(df))
        df.drop_duplicates(subset=["timestamp"], keep="last", inplace=True)
        print("drop后长度:", len(df))
        df_into_db(df, db_name="all_history_ohlcvm_coinmarketcap", table_name="k_line")


        print(f"\n总共获取 {len(df)} 条5分钟K线数据")
        print(f"时间范围: {df.index.min()} 到 {df.index.max()}")

        return df
    else:
        return pd.DataFrame()

# 获取完整数据
historical_data = get_complete_historical_data()